<?php
/**
 * Created by PhpStorm.
 * User: dayu
 * Date: 2019/12/24
 * Time: 14:45
 */

namespace app\api\controller;


use data\model\NsOrderModel;
use data\service\Member;
use think\Controller;
use data\service\ActivityGoods;
use app\Validate\DiscountListValidate;
use data\service\Order\Order;


class Activity extends BaseController
{

    /**
     * 限时购列表
     * @param DiscountListValidate $discountListValidate
     * @param ActivityGoods $activityGoods
     * @return string
     * @throws \Exception
     */
    public function discountList(DiscountListValidate $discountListValidate, ActivityGoods $activityGoods)
    {
        $start_time = $discountListValidate->request->post('start_time');
        $end_time = $discountListValidate->request->post('end_time');
        $pag = $discountListValidate->request->post('pag', 1);
        $number = $discountListValidate->request->post('number', 10);
        $data = $activityGoods->discountList($this->shop_id, $start_time, $end_time, $pag, $number);
        return return_json_data($data, 200);

    }


    /**
     * 分配优惠券
     *
     * @return \multitype
     * @author dzl
     */
    public function getCoupon()
    {
        if (!empty($this->uid)) {
            $coupon_type_id = request()->post('coupon_type_id', 0);
            $uid = request()->post('uid', 0);
            $member = new Member();
            $retval = $member->memberGetCoupon($uid, $coupon_type_id, 2);
            return AjaxReturn($retval);
        } else {
            return AjaxReturn(NO_LOGIN);
        }
    }

    /**
     * 限时购订单创建
     * @param DiscountListValidate $discountListValidate
     */
    public function purchase(DiscountListValidate $discountListValidate)
    {
        $order = new Order();
        $goods_sku_list = $discountListValidate->request->post('goods_sku_list');
        $addres_id = $discountListValidate->request->post('addres_id');
        $buyer_message = $discountListValidate->request->post('buyer_message');
        $coupon_id = $discountListValidate->request->post('coupon_id');
        $out_trade_no = createOutRradeNo(5);

        $user_buy_Info = json_encode([
            'order_type' => 5,
            'goods_sku_list' => json_decode($goods_sku_list, true),
            'addres_id' => $addres_id,
            'buyer_message' => $buyer_message,
            'coupon_id' => $coupon_id,
            'user_id' => $this->user_id,
            'out_trade_no' => $out_trade_no,
        ]);

        $this->redis->rpush('purchase_list', $user_buy_Info);
        $count = $this->redis->lLen("purchase_list");
        for ($i = 0; $i < $count; $i++) {
            $data = $this->redis->lPop('purchase_list');
            $array = json_decode($data,true);
            $boole = $order->orderCreate($array['addres_id'], $array['order_type'], $array['out_trade_no'], $array['buyer_message'], $array['coupon_id'], $array['goods_sku_list'], '', $array['user_id']);
            if(!$boole&&($array['out_trade_no']==$out_trade_no)){
                   return return_json_data("订单创建失败", 500);
            }
        }
        return return_json_data([ 'out_trade_no' => $out_trade_no], 200);
    }


}